# @time 2021/9/8-21:08
# @project 2021-09-04-装饰器作业 
# @author REP超超

"""
作业一：装饰器装饰类
• 定义一个装饰器sorted_by_create_time实现装饰类的装饰器
• 装饰器的功能：按创建时间将实例进行排序
"""
# import functools,time
#
# def sorted_by_create_time(cls):  # 外函数，接受一个类
#     original_init = cls.__init__  # 保留元数据
#
#     @functools.wraps(original_init)  # 保留元数据，保留传递进来的类的元数据，并将它的元数据赋值给内函数
#     def new_init(self,*args,**kwargs):  # 内函数：添加创建时间功能
#         original_init(self,*args,**kwargs)  # 保留原属性
#         self.create_time = time.time()  # 给类添加创建时间属性
#
#     cls.__init__ = new_init  # 修改cls类的属性
#
#     cls.__lt__ = lambda self,other:self.create_time < other.create_time
#     return cls
#
# @sorted_by_create_time
# class A():
#     def __init__(self,sc):
#         self.sc = sc
#         self.create_time = time.time()
#
#     def __repr__(self):  # 对象描述信息
#         return self.sc
#
# sc1= A("SanChuangFeng")
# time.sleep(1)
# sc2 = A("SanChuangWen")
# time.sleep(1)
# sc3 = A("SanChuangEveryone")
# lst1 = [sc3,sc2,sc1]
# sort_time = sorted(lst1)
# print(f"创建实例由早到晚依次为:{[f'{i}' for i in sort_time]}")

"""
作业二：完善三创购物系统
• 所有用户存放在Excel中（用户名，密码，余额）
• 定义装饰器login_required，给需要的功能添加装饰器
• 查看超市物品，不需要登录
• 购物及查看余额，需要登录
• 先验证用户是否已经登录（如果用户未登录提示“用户未登录，请登录后重试”，并跳
转到功能菜单）
"""
# 注：此作业还没完成，最迟星期五上午提交完成此作业
# system_user = {"TaylorSwift":"5201314","Lina":5201314,"chaochao":"5201314"}
# goods = {"F0001":"1989","F0002":"网球拍","F0003":"茶颜悦色"}
# price = {"F0001":520,"F0002":1314,"F0003":16}
# shopping_car = {}
# # login()进行用户登录验证
#
# def login_required(func):  # 验证用户信息的装饰器
#     def check_in(*args,**kwargs):
#         while True:
#             print("请登陆验证".center(20,"*"))
#             username = input("请输入用户名:")
#             passwd = input("请输入密码：")
#             user_mes = (username,passwd)
#             if user_mes in system_user.items():
#                 print("----------------------------------")
#                 print("尊敬的VIP顾客您好，欢迎您购物，有任何问题请联系123，退出请输入\033[0;31;40m exit\033[0m ！")
#                 print("----------------------------------")
#                 func(*args,**kwargs)
#                 break
#             else:
#                 print("***********************************")
#                 print("*您的用户名或密码错误，请确认后重新输入！*")
#                 print("***********************************")
#     return check_in
#
# # recharge（）进行预算管理
# def recharge(money):
#     if money.isdigit():
#         print(f"**恭喜您成功充值{money}￥**。")
#     else:
#         print("你是输入有误，请输入数字。")
#
# # shop()进行购物,prices剩余的预算
# def shop(money):
#     while True:
#         global prices
#         prices = int(money)
#         shopping = input("查看商品请输入1;查看已购信息请输入2；购买商品请输入3。\n")
#         if shopping == "1":
#             for i,j in goods.items():
#                 print(f"商品编号{i}的商品是{j}，商品的单价是{price[i]}￥。")
#         elif shopping == "2":
#             if len(shopping_car) > 0:
#                 for i in shopping_car:
#                     prices -= shopping_car[i]
#                     print(f"您已经购买了{i}")
#                 print(f"您剩余预算{prices}")
#             else:
#                 print("您的购物车为空，请您选择心爱的商品！")
#         elif shopping == "3":
#             num = input("请输入你要购买的商品的编号：")
#             if num in goods.keys():
#                if int(money) >= price[num]:
#                    shopping_car[goods[num]] = price[num]
#                    print(f"您已成功购买{goods[num]}")
#                    continue
#                else:
#                    print("余额不足，请充值")
#             elif num == exit:
#                for i in shopping_car:
#                    print(f"已购买商品{i},余额{prices}")
#                break
#             else:
#                print("您输入的编号有误，请重新输入。")
#             continue
#         else:
#             print("您输入编号有误，请重新输入")
# @login_required
# def add():
#     register = input("是否已有帐号？\n若有账号请输入'0',注册请输入'1'\n")
#     while register == 1:
#         print('*'*10,'请完成注册','*'*10)
#         username = input("请输入用户名：")
#         password = input("password:")
#         li = {}  # 注册的信息
#         li['username'] = username
#         li['password'] = password
#         system_user.append(li)
#         print('注册成功！')
#         break
#     else:
#         print("请进行登录\n")
#
# print("欢迎光临超好吃超好用超超网络购物商城！".center(40,"*"))
# if __name__ == '__main__':
#     add()
"""
作业三：批量扫描指定网段内的存活IP
• 网段：192.168.0.0/24 (192.168.0.1~192.168.0.255)
• 考虑如何才能更快速地扫描出结果
• 前置模块：IPy, ping命令的使用
"""
# import subprocess
# import threading
# def ping(host):
#     rc = subprocess.call(
#         'ping -c2 %s &> /dev/null' % host,
#         shell=True
#     )
#     if rc:
#         print(f"{host}: down")
#     else:
#         print(f"{host}: up")
# if __name__ == '__main__':
#     ips = [f"192.168.0.{i}"  for i in range(1, 255)]
#     for ip in ips:
#         # 创建线程，ping是上面定义的函数, args是传给ping函数的参数
#         t = threading.Thread(target=ping, args=(ip,))
#         t.start()  # 执行ping(ip)
